(function($, gmwk) {

    function Dashboard(Options) {
  
        // console.info("Options", Options);
    
        var self = this;
        var sClassName = Options.sClassName;
        var jqContainer = Options.jqObjectContainer;
        var jqLoader = jqContainer.find(".Dashboard_loader");
        var jqContent = jqContainer.find(".Dashboard_content");
        var jqLanding = jqContainer.find(".Dashboard_landing");
        var sClassName = Options.sClassName;
        var Child = {};
        
        jqContainer.click(fClick);
        fSetupLoader();
        
        //console.info("---------> Dashboard constructed");
        
        this.getClassName = function() {
            return sClassName;
        }
        
        this.route = function(Fragment) {
            //console.info("route");
            // Using instanceof breaks when lazy loading
            var sChildClassName = (Child.getClassName !== undefined) ? Child.getClassName() : "";
            // Page must be specified and a valid class name if not show landing page
            if (
              (Fragment.p === undefined) || 
              (Fragment.p === "") ||
              (gmwk.classes[Fragment.p] === undefined)
            ) {
              fShowLanding();
              return;
            }
            fHideLanding();
            // Set title
            document.title = "GMWK Dashboard " + Fragment.p;
            // Create new Child if menu changed
            if (Fragment.p !== sChildClassName) {
                // Create child
                jqContent.empty();
                Child = gmwk.create(
                    Fragment.p,
                    {
                        sTemplate: Fragment.p,
                        jqParentContainer: jqContent
                    }
                );
                // Toggle menu item
                fToggleMenu(Fragment.p);
            }
            // Route hashchange event to child
            if (Child.route !== undefined) {
                Child.route(Fragment);
            }
        }
            
        function fClick(e) {
            var jqTarget = $(e.target);
            if (gmwk.closestObjectContainer(jqTarget) != jqContainer.attr("id")) {
                return;
            }
            //console.info("fClick");
        }
            
        function fSetupLoader() {
            jqLoader.ajaxStart(
                function() {
                   $(this).show();
                }
            );
            jqLoader.ajaxStop(
                function() {
                    $(this).fadeOut(500);
                }
            );    
        }
        
        function fToggleMenu(sMenu) {
            jqContainer.find(".Dashboard_menu_item").removeClass("global_menu_on");
            jqContainer.find(".Dashboard_menu_item.Dashboard_" + sMenu).addClass("global_menu_on");
        }
        
        function fShowLanding() {
            jqContent.hide();
            jqLanding.show();
        }
        
        function fHideLanding() {
            jqLanding.hide();
            jqContent.show();
        }
      
    }
  
    gmwk.classes.Dashboard = Dashboard;

})(jQuery, window.gmwk);
